#include <func.h>

int glob = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;	// 采用默认属性

void* start_routine(void* arg) {
	int loops = (int)arg;
	for(int i = 0; i < loops; i++) {
		// 上锁
		pthread_mutex_lock(&mutex);
		glob++;	// 对共享变量的操作：临界区
		pthread_mutex_unlock(&mutex);
		// 释放锁
	}
	return NULL;
}

int main(int argc, char* argv[])
{
	pthread_t tid1, tid2;
	
	pthread_create(&tid1, NULL, start_routine, (void*)10000000);
	pthread_create(&tid2, NULL, start_routine, (void*)10000000);

	// 等待tid1, tid2结束
	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);

	// 打印最终结果
	printf("glob = %d\n", glob);
    return 0;
}

